//
//  SISSEscrowUnfreezeFillInViewController.m
//  SISSAPP
//
//  Created by 袁应荣 on 2018/10/26.
//  Copyright © 2018年 优房网. All rights reserved.
//

#import "SISSEscrowUnfreezeFillInViewController.h"
#import "SISSEscrowUnfreezeViewModel.h"
#import "SISSEscrowUnfreezeShowViewController.h"
#import "SISSNetworkingManager+HouseProgress.h"

@interface SISSEscrowUnfreezeFillInViewController ()

/* 确定按钮 */
@property (weak, nonatomic) UIButton *confirmButton;

@property (strong, nonatomic) SISSEscrowUnfreezeViewModel *escrowUnfreezeViewModel;

@end

@implementation SISSEscrowUnfreezeFillInViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"监管解冻";
    
    [self requestData];
}

- (void)requestData {
    
    NSString *ahbpId = self.houseProgressModel ? self.houseProgressModel.ahbpId : self.passengerProgressRecordInfoModel.ahbpId;
    
    [kNetworkManager requestFundSupervisionInfoWithAhbpId:ahbpId success:^(SISSEscrowAccountInfoModel *ecrowAccountInfoModel) {
//        self.ecrowAccountInfoModel = ecrowAccountInfoModel;
        if ([ecrowAccountInfoModel.term integerValue] == 1) {
            self.escrowUnfreezeViewModel.dataArr[0][1].rightStr = @"甲乙双方出具《划转资金划转通知书》";
        } else if ([ecrowAccountInfoModel.term integerValue] == 2) {
            self.escrowUnfreezeViewModel.dataArr[0][1].rightStr = @"交易房产从甲方过户到乙方名下，银行凭留存权利人已登记的乙方产权证书登记复印件划款。";
        } else if ([ecrowAccountInfoModel.term integerValue] == 3) {
            self.escrowUnfreezeViewModel.dataArr[0][1].rightStr = @"甲乙双方就交易向房地产登记部门过户申请。";
        }
        self.escrowUnfreezeViewModel.dataArr[0][3].rightStr = [NSString stringWithFormat:@"达成交易时，利息划转至%@帐户，账户姓名:%@ 账户号码:%@",ecrowAccountInfoModel.interestAccountOwner,ecrowAccountInfoModel.interestAccountName,ecrowAccountInfoModel.interestAccountNum];
        [self.tableView reloadData];
    } failed:^(NSError *error) {
        NSLog(@"%@",error);
    }];
}

#pragma mark - 代理
#pragma mark -- UITableViewDelegate & UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [self.escrowUnfreezeViewModel getSectionCount];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.escrowUnfreezeViewModel getRowsInSection:section];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [self.escrowUnfreezeViewModel getCell:tableView indexPath:indexPath];
    if (indexPath.section == 0 && (indexPath.row == 1 || indexPath.row == 3)) {
        cell.contentView.userInteractionEnabled = NO;
    } else {
       cell.contentView.userInteractionEnabled = YES;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return [self.escrowUnfreezeViewModel getHeightForHeaderInSection:section];
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    return [self.escrowUnfreezeViewModel getViewHeaderInSection:section];
}

// 取消tableView组头悬停
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat sectionHeaderHeight = 40;
    if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
        scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
    } else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
        scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
    }
}


#pragma mark - 按钮点击事件
- (void)confirmButtonClickAction {
    
    SISSEscrowTrawInfoModel *escrowTrawInfoModel = [self.escrowUnfreezeViewModel dataArrToEscrowTrawInfoModel];
    if (escrowTrawInfoModel.thawState.length == 0) {
        [SISSTools showSVErrorTitle:@"请选择解冻情况" andTime:2 andColor:YES];
        return;
    }
    if (!escrowTrawInfoModel.thawTime || escrowTrawInfoModel.thawTime.length == 0) {
        [SISSTools showSVErrorTitle:@"请选择解冻时间" andTime:2 andColor:YES];
        return;
    }
    NSString *interestProcess = escrowTrawInfoModel.interestProcess.length ? escrowTrawInfoModel.interestProcess : @"";
    NSString *thawWay = escrowTrawInfoModel.thawWay.length ? escrowTrawInfoModel.thawWay : @"";
    NSString *otherdesc = escrowTrawInfoModel.otherdesc.length ? escrowTrawInfoModel.otherdesc : @"";
    NSString *thawTime = [escrowTrawInfoModel.thawTime stringByReplacingOccurrencesOfString:@"-" withString:@"/"];
    
    NSString *ahbpId = self.houseProgressModel ? self.houseProgressModel.ahbpId : self.passengerProgressRecordInfoModel.ahbpId;
    NSString *adbpId = self.houseProgressModel ? self.houseProgressRecordInfoModel.adbpId : self.passengerProgressModel.adbpId;
    NSString *ahrId = self.houseProgressModel ? self.houseProgressModel.house.ID : @"";
    NSString *abdId = self.houseProgressModel ? @"" : self.passengerProgressModel.demand.ID;
    
    if (self.escrowTrawInfoModel) {
        NSDictionary *parameters = @{
                                     @"id": self.escrowTrawInfoModel.ID,
                                     @"spId": [SISSUserAccount shared].spId,
                                     @"ahbpId": ahbpId,
                                     @"adbpId": adbpId,
                                     @"ahrId": ahrId,
                                     @"abdId": abdId,
                                     @"demandType": @(self.houseProgressRecordInfoModel.demand_type),
                                     @"houseType": @(self.houseProgressModel.houseresType),
                                     @"interestProcess": interestProcess,
                                     @"otherdesc": otherdesc,
                                     @"thawState": escrowTrawInfoModel.thawState,
                                     @"thawTime": thawTime,
                                     @"thawWay": thawWay
                                     };
        [kNetworkManager updateThawInfoWithParamters:parameters success:^(id responseObject) {
            if ([responseObject[@"code"] integerValue] == 200) {
                SISSEscrowUnfreezeShowViewController *vc = [[SISSEscrowUnfreezeShowViewController alloc] init];
                vc.houseProgressModel = self.houseProgressModel;
                vc.houseProgressRecordInfoModel = self.houseProgressRecordInfoModel;
                vc.passengerProgressModel = self.passengerProgressModel;
                vc.passengerProgressRecordInfoModel = self.passengerProgressRecordInfoModel;
                [self.navigationController pushViewController:vc animated:YES];
            }
        } failed:^(NSError *error) {
            NSLog(@"%@",error);
        }];
    } else {
        NSDictionary *parameters = @{
                                     @"spId": [SISSUserAccount shared].spId,
                                     @"ahbpId": ahbpId,
                                     @"adbpId": adbpId,
                                     @"ahrId": ahrId,
                                     @"abdId": abdId,
                                     @"demandType": @(self.houseProgressRecordInfoModel.demand_type),
                                     @"houseType": @(self.houseProgressModel.houseresType),
                                     @"interestProcess": interestProcess,
                                     @"otherdesc": otherdesc,
                                     @"thawState": escrowTrawInfoModel.thawState,
                                     @"thawTime": thawTime,
                                     @"thawWay": thawWay
                                     };
        [kNetworkManager insertThawInfoWithParamters:parameters success:^(id responseObject) {
            if ([responseObject[@"code"] integerValue] == 200) {
                SISSEscrowUnfreezeShowViewController *vc = [[SISSEscrowUnfreezeShowViewController alloc] init];
                vc.houseProgressModel = self.houseProgressModel;
                vc.houseProgressRecordInfoModel = self.houseProgressRecordInfoModel;
                vc.passengerProgressModel = self.passengerProgressModel;
                vc.passengerProgressRecordInfoModel = self.passengerProgressRecordInfoModel;
                [self.navigationController pushViewController:vc animated:YES];
            }
        } failed:^(NSError *error) {
            NSLog(@"%@",error);
        }];
    }
}


#pragma mark - UI
- (void)setupSubviews {
    [super setupSubviews];
    
    self.escrowUnfreezeViewModel = [[SISSEscrowUnfreezeViewModel alloc] init];
    if (self.escrowTrawInfoModel) {
        [self.escrowUnfreezeViewModel assignmentWithEscrowTrawInfoModel:self.escrowTrawInfoModel];
    }
    SISSBaseTableView *tableView = [[SISSBaseTableView alloc] init];
    tableView.estimatedRowHeight = 44;
    tableView.rowHeight = UITableViewAutomaticDimension;
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.escrowUnfreezeViewModel registContractClass:tableView];
    [self.view addSubview:tableView];
    self.tableView = tableView;
    self.escrowUnfreezeViewModel.tableView = tableView;
    
    UIView *footerView = [[UIView alloc ] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 88)];
    
    UIButton *confirmButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [confirmButton setTitle:@"确定" forState:UIControlStateNormal];
    [confirmButton setBackgroundColor:MAIN_COLOR];
    [confirmButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [confirmButton addTarget:self action:@selector(confirmButtonClickAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:confirmButton];
    self.confirmButton = confirmButton;
    
    [self.confirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.bottom.equalTo(self.mas_bottomLayoutGuideTop);
        //        make.width.mas_equalTo(SCREEN_WIDTH);
        make.height.mas_equalTo(44);
    }];
    
    self.tableView.tableFooterView = footerView;
}

@end
